<?php
class NoticiasController extends AppController {

	var $name = 'Noticias';
	var $helpers = array('Html', 'Form', 'Javascript', 'Time','Flash');

	function index() {
		$this->Noticia->recursive = 0;
		$this->set('noticias', $this->Noticia->find('all', array('conditions'=>array('activo'),'order'=>'orden ASC')));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Noticia Invalida.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('noticia', $this->Noticia->read(null, $id));
	}




	function admin_index() {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		$this->Noticia->recursive = 0;
		$this->set('noticias', $this->paginate());
	}

	function admin_view($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id) {
			$this->Session->setFlash(__('Invalid Noticia.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('noticia', $this->Noticia->read(null, $id));
	}

	function admin_add() {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!empty($this->data)) {
			$this->Noticia->create();
			if ($this->Noticia->save($this->data)) {
				$this->Session->setFlash(__('Se Guardo la noticia', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('No se pudo guardar la noticia.', true));
			}
		}
	}

	function admin_edit($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Noticia', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Noticia->save($this->data)) {
				$this->Session->setFlash(__('The Noticia has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Noticia could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Noticia->read(null, $id);
		}
	}

	function admin_delete($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Noticia', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Noticia->del($id)) {
			$this->Session->setFlash(__('Noticia deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
